<?php
require_once ("interfaces/iperson_controller.php");
require_once ("secure_area.php");

class Customers extends Secure_area
{
	function __construct()
	{
		parent::__construct('customers');
		$this->load->model('District');
	}
	
	function index()
	{
		$config['base_url'] = site_url('/customers/index');
		$config['total_rows'] = $this->Customer->count_all();
		$config['per_page'] = '20';
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		
		$data['controller_name']=strtolower(get_class());
		$data['form_width']=$this->get_form_width();
		$data['manage_table']=get_customer_manage_table( $this->Customer->get_all( $config['per_page'], $this->uri->segment( $config['uri_segment'] ) ), $this );
		$this->load->view('people/manage',$data);
	}
	
	function get_row()
	{
		$item_id = $this->input->post('row_id');
		$data_row=get_customer_data_row($this->Customer->get_info($item_id),$this);
		echo $data_row;
	}
	
	function generate_barcodes($person_ids)
	{
		$result = array();

		$person_ids = explode(':', $person_ids);

		foreach ($person_ids as $item_id)
		{
			$item_info = $this->Customer->get_info($item_id);

			$result[] = array('name' =>$item_info->first_name.' '.$item_info->last_name.' - '.$item_info->account_number, 'id'=> $item_info->account_number);
		}

		$data['items'] = $result;
		$this->load->view("barcode_sheet", $data);
	}
	
	/*
	Returns customer table data rows. This will be called with AJAX.
	*/
	function search()
	{
		$search=$this->input->post('search');
		$data_rows=get_customer_manage_table_data_rows($this->Customer->search($search),$this);
		echo $data_rows;
	}
	
	/*
	Gives search suggestions based on what is being searched for
	*/
	function suggest()
	{
		$suggestions = $this->Customer->get_search_suggestions($this->input->post('q'),$this->input->post('limit'));
		echo implode("\n",$suggestions);
	}
	
	/*
	Loads the customer edit form
	*/
	function view($customer_id=-1)
	{
		$data['person_info']=$this->Customer->get_info($customer_id);
		$this->load->view("customers/form",$data);
	}
	
	/*
	Inserts/updates a customer
	*/
	function save($customer_id=-1)
	{
		$customer_data=array(
				'first_name' => $this->input->post ( 'first_name' ),
				'last_name' => $this->input->post ( 'last_name' ),
				'phone_number' => $this->input->post ( 'phone_number' ) == '' ? null : $this->input->post ( 'phone_number' ),
				'address_1' => $this->input->post ( 'address_1' ) == '' ? null : $this->input->post ( 'address_1' ),
				'dist_id' => $this->input->post ( 'dist_id' ) == '' ? null : $this->input->post ( 'dist_id' ),
				'ssn' => $this->input->post ( 'ssn' ),
				'dob' => $this->input->post ( 'dob' ) == '' ? null : $this->input->post ( 'dob' ),
				'account_number' => 'A'.$this->input->post ( 'ssn' ),
				'point' => $this->input->post ( 'point' ) == '' ? 0 : $this->input->post ( 'point' ),
		);
		
		$this->form_validation->set_rules('ssn','SSN','numeric|required');
		$this->form_validation->set_rules('first_name','First Name','alpha_dash|required');
		$this->form_validation->set_rules('last_name','Last Name','alpha_dash|required');
		
		////
		
		if(!$this->form_validation->run() == FALSE && $this->Customer->save($customer_data,$customer_id))
		{
			//New customer
			if($customer_id==-1)
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('customers_successful_adding').' '.
				$customer_data['first_name'].' '.$customer_data['last_name'],'person_id'=>$customer_data['person_id']));
			}
			else //previous customer
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('customers_successful_updating').' '.
				$customer_data['first_name'].' '.$customer_data['last_name'],'person_id'=>$customer_id));
			}
		}
		else//failure
		{	
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('customers_error_adding_updating').' '.
			$customer_data['first_name'].' '.$customer_data['last_name'],'person_id'=>-1));
		}
	}
	
	/*
	This deletes customers from the customers table
	*/
	function delete()
	{
		$customers_to_delete=$this->input->post('ids');
		
		if($this->Customer->delete_list($customers_to_delete))
		{
			echo json_encode(array('success'=>true,'message'=>$this->lang->line('customers_successful_deleted').' '.
			count($customers_to_delete).' '.$this->lang->line('customers_one_or_multiple')));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('customers_cannot_be_deleted')));
		}
	}
	
	function excel()
	{
		$data = file_get_contents("import_customers.csv");
		$name = 'import_customers.csv';
		force_download($name, $data);
	}
	
	function excel_import()
	{
		$this->load->view("customers/excel_import", null);
	}

	function do_excel_import()
	{

	}
	
	/*
	get the width for the add/edit form
	*/
	function get_form_width()
	{			
		return 350;
	}
}
?>